home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGASIC / BASICTUT.LZH / FORNEXT.BAS < prev    next >
BASIC Source File  |  1980-01-01  |  11KB  |  226 lines

  1. 10 ' MARK A. SWANSON     02-27-1985  20:22:35 
  2. 20 CLS
  3. 30 PRINT:PRINT:PRINT
  4. 50 PRINT TAB(40);10;"F"
  5. 60 FOR A=9 TO 1 STEP -1
  6. 70 READ A$
  7. 80 PRINT TAB(40);A;" ";A$
  8. 90 FOR I=1 TO 100:NEXT I
  9. 100 NEXT A
  10. 110 DATA O,R, ,/, ,N,E,X,T
  11. 120 PRINT
  12. 130 PRINT "------------------------------------------------------------------------------"
  13. 140 PRINT:PRINT:PRINT
  14. 150 PRINT "                                      FOR / NEXT
  15. 160 PRINT
  16. 170 PRINT "                           THE STATEMENT YOU CAN 'COUNT' ON
  17. 180 FOR A=1 TO 2000:NEXT A
  18. 190 CLS
  19. 200 PRINT "             FOR/NEXT is a nifty STATEMENT that has three uses.....
  20. 210 PRINT
  21. 220 PRINT "                          1.  To delay program flow or pause
  22. 230 PRINT "                          2.  To count numbers, forward or backwards
  23. 240 PRINT "                          3.  To PRINT mulitple STRING data
  24. 250 PRINT
  25. 260 PRINT "             We'll look at all of these individually and show how FOR/NEXT
  26. 270 PRINT "             can be used to 'dress up' a program and save programming
  27. 280 PRINT "             time.
  28. 290 PRINT
  29. 300 PRINT "             But first, how it works.....  FOR/NEXT is actually two
  30. 310 PRINT "             statements (on their own lines).  We use FOR/NEXT
  31. 320 PRINT "             to create a controlled loop.  A controlled loop consists
  32. 330 PRINT "             of a starting point and specified goal (the FOR part of
  33. 340 PRINT "             the statement), and the signal to continue and ultimately
  34. 350 PRINT "             end the loop (the NEXT part of the statement).  And,
  35. 360 PRINT "             depending on the particular use, there can be another
  36. 370 PRINT "             statement in between FOR and NEXT that is executed as
  37. 380 PRINT "             many times as is indicated in the FOR part. 
  38. 390 PRINT
  39. 400 PRINT "             Before total confusion sets in, let's have a look at how
  40. 410 PRINT "             FOR/NEXT is set up for each of the three uses listed above.
  41. 420 PRINT
  42. 430 INPUT "                   PRESS [RETURN] TO CONTINUE  [Q] MAIN BTMENU ",Z$
  43. 440 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  44. 450 CLS
  45. 460 PRINT "                              FOR/NEXT AS A DELAY
  46. 470 PRINT
  47. 480 PRINT "          What is meant by DELAY is a pause or interuption in the
  48. 490 PRINT "          program where evrything comes to a complete halt.  This is
  49. 500 PRINT "          the purest form of FOR/NEXT where the computer is instructed
  50. 510 PRINT "          to cease operation while it counts to itself to a specified
  51. 520 PRINT "          number.  Here's an example.....
  52. 530 PRINT
  53. 540 PRINT "                              10 FOR A=1 TO 2000
  54. 550 PRINT "                              20 NEXT A
  55. 560 PRINT
  56. 570 PRINT "          In line 10, the computer sets the scene to count from 1 to
  57. 580 PRINT "          2000.  Notice that we have a modified use of LET, where 'A'
  58. 590 PRINT "          is being used to store the numbers 1 through 2000.
  59. 600 PRINT
  60. 610 PRINT "          In line 20, the computer is instructed to get the NEXT 'A'
  61. 620 PRINT "          to be found back in line 10.  Thus, the loop begins.
  62. 630 PRINT
  63. 640 PRINT "          When the computer has registered all of the numbers (counted
  64. 650 PRINT "          to 2000), there is no 'NEXT A', as they have all been used up.
  65. 660 PRINT "          So, the computer moves on to the next line in the program or
  66. 670 PRINT "          ends if no more lines exist.
  67. 680 PRINT
  68. 690 INPUT "           PRESS [RETURN] TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  69. 700 IF Z$="P" THEN GOTO 190
  70. 710 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  71. 720 CLS
  72. 730 PRINT "             Delays are useful in a variety of situations.  In the 
  73. 740 PRINT "             openning of this lesson you saw a countdown along with
  74. 750 PRINT "             the letters of FOR/NEXT.  FOR/NEXT was used to 'slow down'
  75. 760 PRINT "             the countdown by instructing the computer to count to one
  76. 770 PRINT "             hundred between each number and letter.
  77. 780 PRINT
  78. 790 PRINT "             We'll run an example of the delaying ability of FOR/NEXT.
  79. 800 PRINT 
  80. 810 PRINT "                                    10 FOR T=1 TO 2000
  81. 820 PRINT "                                    20 NEXT T
  82. 830 PRINT
  83. 840 PRINT "             Type RUN and press [RETURN] to start the program.  Note
  84. 850 PRINT "             how long it takes to finish counting and hence, allow the
  85. 860 PRINT "             the rest of this page to be printed.  OK - Type RUN and
  86. 870 PRINT "             press [RETURN]";:INPUT " ",X$
  87. 880 IF X$="RUN" THEN FOR A=1 TO 2000:NEXT A
  88. 890 IF X$<>"RUN" THEN PRINT "TRY AGAIN - TYPE RUN AND PRESS [RETURN]":INPUT X$:GOTO 880
  89. 900 PRINT 
  90. 910 PRINT "            You made it !  As you can see, the computer counts to
  91. 920 PRINT "            itself at a very fast clip.  An 'internal' count like this
  92. 930 PRINT "            one to 2000 takes about 5 seconds.
  93. 940 PRINT:PRINT:PRINT
  94. 950 INPUT "          PRESS [RETURN] TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  95. 960 IF Z$="P" THEN GOTO 450
  96. 970 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  97. 980 CLS
  98. 990 PRINT "                            FOR/NEXT AS A NUMBER COUNTER
  99. 1000 PRINT
  100. 1010 PRINT "            We know that the computer can count from and to a specified
  101. 1020 PRINT "            number (i.e  1 to 2000).  Well, why not have the computer
  102. 1030 PRINT "            PRINT the numbers as it counts ?  This is accomplished
  103. 1040 PRINT "            with the addition of a PRINT statement.  Here's how.....
  104. 1050 PRINT
  105. 1060 PRINT "                               10 FOR G=1 TO 5
  106. 1070 PRINT "                               20 PRINT G
  107. 1080 PRINT "                               30 NEXT G
  108. 1090 PRINT
  109. 1100 INPUT "            Ok, type RUN and press [RETURN] to see what happens ",Z$
  110. 1110 IF Z$="RUN" THEN FOR G=1 TO 5:PRINT TAB(40); G:NEXT G
  111. 1120 IF Z$<>"RUN" THEN INPUT "TRY AGAIN - Type RUN and press [RETURN] ",Z$:GOTO 1110
  112. 1130 FOR A=1 TO 500:NEXT A:PRINT
  113. 1140 PRINT "           We can also count 'by' numbers. That is, the computer can
  114. 1150 PRINT "           count to 10 by 2's or 3's or even 6.45375's, with the STEP
  115. 1160 PRINT "           function added on.
  116. 1170 PRINT:PRINT
  117. 1180 INPUT "          PRESS [RETURN] TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  118. 1190 IF Z$="P" THEN GOTO 720
  119. 1200 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  120. 1210 CLS
  121. 1220 PRINT "           Below is a program that uses STEP to count to 10 by 2's.....
  122. 1230 PRINT "                 (note the comma (,) after the K in line 20)
  123. 1240 PRINT
  124. 1250 PRINT "                               10 FOR K=2 TO 10 STEP 2
  125. 1260 PRINT "                               20 PRINT K,
  126. 1270 PRINT "                               30 NEXT K
  127. 1280 PRINT
  128. 1290 INPUT "           Again, type RUN and press [RETURN]  ",Z$
  129. 1300 PRINT
  130. 1310 IF Z$="RUN" THEN FOR K=2 TO 10 STEP 2:PRINT K,:NEXT K
  131. 1320 IF Z$<>"RUN" THEN INPUT "TRY AGAIN - Type RUN and press [RETURN] ",Z$:GOTO 1310
  132. 1330 FOR A=1 TO 500:NEXT A
  133. 1340 PRINT:PRINT:PRINT
  134. 1350 PRINT "           Now let's make the computer count backwards.....
  135. 1360 PRINT
  136. 1370 PRINT "                               10 FOR A=20 TO 1 STEP -1
  137. 1380 PRINT "                               20 PRINT A;
  138. 1390 PRINT
  139. 1400 INPUT "           Type RUN and press [RETURN]  ",Z$:PRINT
  140. 1410 FOR A=20 TO 1 STEP -1:PRINT A;:NEXT A
  141. 1420 FOR A=1 TO 500:NEXT A
  142. 1430 PRINT:PRINT:PRINT
  143. 1440 INPUT "         PRESS [RETURN] TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  144. 1450 IF Z$="P" THEN GOTO 980
  145. 1460 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  146. 1470 CLS
  147. 1480 PRINT "                       FOR/NEXT TO PRINT MULTIPLE DATA
  148. 1490 PRINT
  149. 1500 PRINT "            FOR/NEXT can cause the computer to repeatedly PRINT data
  150. 1510 PRINT "            as many times as is specified in the FOR line.
  151. 1520 PRINT
  152. 1530 PRINT "                               10 FOR J=1 TO 3
  153. 1540 PRINT "                               20 PRINT ''HI THERE !''
  154. 1550 PRINT "                               30 NEXT J
  155. 1560 PRINT
  156. 1570 INPUT "           Type RUN and press [RETURN] ",Z$
  157. 1580 PRINT
  158. 1590 PRINT "                                HI THERE !
  159. 1600 PRINT "                                HI THERE !
  160. 1610 PRINT "                                HI THERE !
  161. 1620 PRINT
  162. 1630 FOR A=1 TO 500:NEXT A
  163. 1640 PRINT "           Instead of PRINTing a variable (in this case 'J'), the
  164. 1650 PRINT "           computer was instructed to print the STRING (HI THERE !).
  165. 1660 PRINT:PRINT:PRINT:PRINT
  166. 1670 INPUT "          PRESS [RETURN] TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  167. 1680 IF Z$="P" THEN GOTO 1210
  168. 1690 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  169. 1700 CLS
  170. 1710 PRINT "                            ALL IN ONE PACKAGE.....
  171. 1720 PRINT
  172. 1730 PRINT "            As a sort of 'grand finale' we'll use each application of
  173. 1740 PRINT "            FOR/NEXT all in one program.  When we combine FOR/NEXT loops,
  174. 1750 PRINT "            we NEST them, and thus call them NESTED LOOPS.
  175. 1760 PRINT
  176. 1770 PRINT "                              10 FOR R=1 TO 5
  177. 1780 PRINT "                              20 PRINT R;''BASIC''
  178. 1790 PRINT "                              30 FOR S=1 TO 500
  179. 1800 PRINT "                              40 NEXT S
  180. 1810 PRINT "                              50 NEXT R
  181. 1820 PRINT
  182. 1830 PRINT "           Lines 20, 30, and 40 are all enclosed in the main loop and
  183. 1840 PRINT "           will be repeated 5 times. 
  184. 1850 PRINT
  185. 1860 PRINT "           Line 20 will PRINT the number stored to the VARIABLE 'R'
  186. 1870 PRINT "           and, on the same line, the STRING 'BASIC'.
  187. 1880 PRINT
  188. 1890 PRINT "           Lines 30 and 40 will cause a pause (for an internal count
  189. 1900 PRINT "           to 500).
  190. 1910 PRINT:PRINT:PRINT
  191. 1920 INPUT "          PRESS [RETURN TO CONTINUE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  192. 1930 IF Z$="P" THEN GOTO 1470
  193. 1940 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  194. 1950 CLS
  195. 1960 INPUT "Type RUN and press [RETURN] to see the NESTED LOOP program  ",Z$
  196. 1970 PRINT
  197. 1980 FOR A=1 TO 5
  198. 1990 PRINT A;"BASIC"
  199. 2000 FOR T=1 TO 500:NEXT T
  200. 2010 NEXT A
  201. 2020 FOR A=1 TO 1000:NEXT A
  202. 2030 PRINT
  203. 2040 PRINT "------------------------------------------------------------------------------"
  204. 2050 PRINT "IN SUMMARY.....
  205. 2060 PRINT
  206. 2070 PRINT "          1.  FOR/NEXT has three uses:
  207. 2080 PRINT "            a. pause   b. count numbers   c. repeat STRING data
  208. 2090 PRINT "          2.  A NESTED LOOP is a combination of FOR/NEXT loops
  209. 2100 PRINT
  210. 2110 PRINT "------------------------------------------------------------------------------"
  211. 2120 PRINT "FOR PRACTICE.....
  212. 2130 PRINT
  213. 2140 PRINT "          1.  Try each application separately, then combine them
  214. 2150 PRINT:PRINT
  215. 2160 INPUT "            PRESS [Z] TO PRACTICE  [P] PREVIOUS PAGE  [Q] MAIN BTMENU ",Z$
  216. 2170 IF Z$="P" THEN GOTO 1700
  217. 2180 IF Z$="Q" THEN LOAD"B:BTMENU.BAS",R
  218. 2190 CLS
  219. 2200 PRINT "              To begin practicing, type NEW and press [RETURN]
  220. 2210 PRINT
  221. 2220 PRINT "    To get back to lessons, type LOAD''B:BTMENU.BAS'',R AND PRESS [RETURN]
  222. 2230 PRINT
  223. 2240 PRINT "        Remember to use quotations instead of double apostrophies ('')
  224. 2250 PRINT "------------------------------------------------------------------------------"
  225. tead of double apostrophies ('')
  226. 2250 PRINT "-----------------